package com.vgulu.server.common.mongodb;

import com.mongodb.client.model.Filters;
import org.bson.conversions.Bson;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by wangchunsee on 2017/6/18.
 */
public class SmartFilter {
    private List<Bson> filters = new ArrayList<>();

    public void addFilter(Bson filter){
        filters.add(filter);
    }

    public void addFilters(Collection<Bson> filter){
        filters.addAll(filter);
    }

    public Bson and(){
        int size = filters.size();
        if (size==0){
            return null;
        }else if (size==1){
            return filters.get(0);
        }else {
            return Filters.and(filters);
        }
    }

    public Bson or(){
        int size = filters.size();
        if (size==0){
            return null;
        }else if (size==1){
            return filters.get(0);
        }else {
            return Filters.or(filters);
        }
    }

    public Bson nor(){
        int size = filters.size();
        if (size==0){
            return null;
        }else if (size==1){
            return filters.get(0);
        }else {
            return Filters.nor(filters);
        }
    }

}
